package cn.laixueit.controller;

import cn.laixueit.pojo.Cart;
import cn.laixueit.pojo.User;
import cn.laixueit.result.Result;
import cn.laixueit.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/addCart")
    @ResponseBody
    public Result addCart(Long id, HttpSession session){
        User u = (User) session.getAttribute("u");
        if(u==null){
            return new Result(400,"没有登录");
        }
        cartService.addCart(id,u.getId());
        return new Result();
    }


    @GetMapping("/cartList")
    @ResponseBody
    public Result cartList(String username){
        List<Cart> cartList = cartService.cartList(username);
        return new Result(200,cartList);
    }

    @GetMapping("/changeNum")
    @ResponseBody
    public Result changeNum(Integer num,Integer id){
        cartService.changeNum(num,id);
        return new Result();
    }
}
